package com.sky.controller.admin;

import com.sky.dto.OrderPageQueryDTO;
import com.sky.dto.OrderStatusDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理员订单管理控制器
 */
@RestController
@RequestMapping("/admin/order")
@Api(tags = "管理员订单管理相关接口")
public class AdminOrderController {

    private static final Logger log = LoggerFactory.getLogger(AdminOrderController.class);
    
    @Autowired
    private OrderService orderService;

    /**
     * 订单分页查询
     * @param page 页码
     * @param pageSize 每页显示数量
     * @param status 订单状态
     * @param orderNo 订单编号
     * @param studentId 学生ID
     * @param merchantId 商家ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 分页结果
     */
    @ApiOperation("订单分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Long merchantId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        log.info("订单分页查询，参数：page={}, pageSize={}, status={}, orderNo={}, studentId={}, merchantId={}, startDate={}, endDate={}",
                page, pageSize, status, orderNo, studentId, merchantId, startDate, endDate);
        
        OrderPageQueryDTO queryDTO = new OrderPageQueryDTO();
        queryDTO.setPage(page);
        queryDTO.setPageSize(pageSize);
        queryDTO.setStatus(status);
        queryDTO.setOrderNo(orderNo);
        queryDTO.setStudentId(studentId);
        queryDTO.setMerchantId(merchantId);
        queryDTO.setStartDate(startDate);
        queryDTO.setEndDate(endDate);
        
        PageResult pageResult = orderService.adminPageQuery(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 订单详情查看
     * @param id 订单ID
     * @return 订单详情
     */
    @ApiOperation("订单详情查看")
    @GetMapping("/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable Long id) {
        log.info("订单详情查看，订单ID：{}", id);
        OrderVO orderVO = orderService.getOrderById(id);
        return Result.success(orderVO);
    }

    /**
     * 订单状态更新
     * @param orderStatusDTO 订单状态更新DTO
     * @return 操作结果
     */
    @ApiOperation("订单状态更新")
    @PutMapping("/status")
    public Result<String> updateOrderStatus(@RequestBody OrderStatusDTO orderStatusDTO) {
        log.info("订单状态更新，订单ID：{}，状态：{}", orderStatusDTO.getId(), orderStatusDTO.getStatus());
        orderService.updateOrderStatus(orderStatusDTO);
        return Result.success("订单状态更新成功");
    }

    /**
     * 订单搜索筛选
     * @param orderNo 订单编号
     * @param studentId 学生ID
     * @param merchantId 商家ID
     * @param status 订单状态
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 订单列表
     */
    @ApiOperation("订单搜索筛选")
    @GetMapping("/search")
    public Result<PageResult> searchOrders(
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Long merchantId,
            @RequestParam(required = false) String studentName,
            @RequestParam(required = false) String merchantName,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        log.info("订单搜索筛选，参数：orderNo={}, studentId={}, merchantId={}, studentName={}, merchantName={}, status={}, startDate={}, endDate={}",
                orderNo, studentId, merchantId, studentName, merchantName, status, startDate, endDate);
        
        OrderPageQueryDTO queryDTO = new OrderPageQueryDTO();
        queryDTO.setOrderNo(orderNo);
        queryDTO.setStudentId(studentId);
        queryDTO.setMerchantId(merchantId);
        queryDTO.setStudentName(studentName);
        queryDTO.setMerchantName(merchantName);
        queryDTO.setStatus(status);
        queryDTO.setStartDate(startDate);
        queryDTO.setEndDate(endDate);
        queryDTO.setPage(1);
        queryDTO.setPageSize(100); // 搜索结果默认返回前100条
        
        PageResult pageResult = orderService.adminPageQuery(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取订单统计数据
     * @return 统计数据
     */
    @ApiOperation("获取订单统计数据")
    @GetMapping("/stats")
    public Result<Map<String, Object>> getOrderStats() {
        log.info("获取订单统计数据");
        Map<String, Object> stats = orderService.getOrderStats();
        return Result.success(stats);
    }
}